/*学生获奖信息统计 */
<template>
  <div>
    <condition-card @reset="reset" @search="onSubmit" label-position="right" label-width="150px">
      <el-row style="height: 50px;">
        <!-- 获奖类别 --> 
        <el-col :span="8">
          <el-form-item :label="$t('statisticsOfStudentsAwards.awardCategories')">
            <!-- <DictSelect v-model="queryForm.condition.comLevel" filterable @change="collegeChange" :data="options.title" :plzSelect="true"></DictSelect>           -->
            <el-select 
              v-model="queryForm.condition.comLevel"
              clearable
              style="width: 100%">
              <el-option
                v-for="item in options.title"
                :key="item.id"
                :label="item.label"
                :value="item.label">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col> 
        <!-- 竞赛项目名称 -->
        <el-col :span="8">
          <el-form-item :label="$t('statisticsOfStudentsAwards.competitionName')">
            <!-- <el-select collapse-tags  v-model="queryForm.condition.projectName"  filterable clearable  style="width: 100%">
              <el-option  v-for="item in options.profession" :key="item.code" :label="item.label" :value="item.value"></el-option>
            </el-select> -->
            <el-input v-model="queryForm.condition.projectName" clearable  :placeholder="$t('statisticsOfStudentsAwards.pleaseEnter')"></el-input>
          </el-form-item>
        </el-col>
        <!-- 竞赛级别 -->
        <!-- <el-col :span="8">
          <el-form-item :label="$t('statisticsOfStudentsAwards.competitionLevel')">
            <el-select v-model="queryForm.condition.awardsName"  clearable style="width: 100%">
              <el-option v-for="item in options.batch" :key="item.id" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <!-- 获奖时间 -->
        <!-- <el-col :span="8">
          <el-form-item :label="$t('statisticsOfStudentsAwards.awardTime')">
            <el-date-picker
              style="width: 100%"
              v-model="queryForm.condition.awardsDateStart"
              type="monthrange"
              range-separator="至"
              :start-placeholder="$t('statisticsOfStudentsAwards.awardsDateStart')"
              :end-placeholder="$t('statisticsOfStudentsAwards.awardsDateEnd')">
            </el-date-picker>
          </el-form-item>
        </el-col> -->
        <!-- 获奖学生 -->
        <el-col :span="8">
          <el-form-item :label="$t('statisticsOfStudentsAwards.awardWinningStudents')">
            <el-input v-model="queryForm.condition.studentId"
             :placeholder="$t('statisticsOfStudentsAwards.pleaseInputStudentNumberOrName')"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
      <!-- 获奖时间 -->
      <el-form-item :label="$t('statisticsOfStudentsAwards.awardTime')">
        <el-col :span="3">
            <el-form-item >
                <el-date-picker 
                :picker-options="pickerOptionsStart"
                type="month" 
                :placeholder="$t('statisticsOfStudentsAwards.awardsDateStart')"
                  v-model="queryForm.condition.awardsDateStart"
                style="width: 100%;"></el-date-picker>
            </el-form-item>
        </el-col>
        <el-col class="line" style="margin:0 4px 0 4px" :span="0.1">至</el-col>
        <el-col :span="3">
        <el-form-item>
            <el-date-picker 
            :picker-options="pickerOptionsEnd"
              type="month" 
              :placeholder="$t('statisticsOfStudentsAwards.awardsDateEnd')"
                v-model="queryForm.condition.awardsDateEnd" 
                style="width: 100%;"></el-date-picker>
        </el-form-item>
        </el-col>
    </el-form-item>
        <!-- <el-col :span="2">
          <el-form-item :label="$t('statisticsOfStudentsAwards.awardTime')">
            <el-date-picker
              v-model="queryForm.condition.awardsDateStart"
              type="month"
              :placeholder="$t('statisticsOfStudentsAwards.awardsDateStart')">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" style="margin-left:30px" :span="1">
          <span>12312312312</span>
        </el-col>
        <el-col :span="2">
          <el-form-item >
            <el-date-picker
              v-model="queryForm.condition.awardsDateEnd"
              type="month"
              :placeholder="$t('statisticsOfStudentsAwards.awardsDateEnd')">
            </el-date-picker>
          </el-form-item>
        </el-col> -->
      </el-row>
    </condition-card>



    <el-card shadow="never" v-loading="loading" body-style="padding: 24px;" class="card-margin">
      <h1 class="condition-title">{{$t('statisticsOfStudentsAwards.statisticalListOfStudentAwardInformation')}}</h1>
        <el-row style="padding:15px;" class="row-bg" justify="space-between">
          <el-button
              type="primary"
              style="float:right; margin-left:10px;"
              @click="exportFile"
            >{{$t('statisticsOfStudentsAwards.export')}}</el-button>
        </el-row>

      <edu-table :pagination="false" :selection='false' :tooltipEffect="tooltipEffect" stripe :data="dataList" size="mini" @selection-change="handleSelectionChange">
        <edu-table-column :label="$t('statisticsOfStudentsAwards.studentNumber')" prop="studentId"></edu-table-column>
        <edu-table-column :label="$t('statisticsOfStudentsAwards.fullName')" prop="studentName"></edu-table-column>
        <edu-table-column :label="$t('statisticsOfStudentsAwards.competitionName')" prop="projectName"></edu-table-column>
        <edu-table-column :label="$t('statisticsOfStudentsAwards.awardTime')" prop="awardsDate"></edu-table-column>
        <edu-table-column :label="$t('statisticsOfStudentsAwards.awardCategories')" prop="comLevel"></edu-table-column>
        <edu-table-column :label="$t('statisticsOfStudentsAwards.awardLevel')" prop="awardsName"></edu-table-column>
        <edu-table-column :label="$t('statisticsOfStudentsAwards.titleOfAwardWinningWorks')" prop="projectTitle"></edu-table-column>
      </edu-table>

      <el-row type="flex" class="row-bg" justify="end">
        <el-col>
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryForm.pageNum_"
              :page-sizes="[20, 40, 80, 100]"
              :page-size="queryForm.pageSize_"
              layout="total, sizes, prev, pager, next, jumper"
              :total="queryForm.total_"
              style="float:right;margin-top:10px"
            ></el-pagination>
        </el-col>
      </el-row>
    </el-card>
    <edu-export-dialog
      :disabled="exportButton"
      :title="$t('message.prompt')"
      ref="downloadRef"
      :data="exportResult">
    </edu-export-dialog>
  </div>
</template>


<script>
import { queryDic } from "common/src/api/dictionary";
import UTILS from "common/src/utils/utils";
import competiManagementApi from 'common/src/api/subjectCompetition/competiManagement';
// import mainbaseView from 'common/src/views/graduateStudent/component/mainbaseView';
// import addDialog from 'common/src/views/graduateStudent/component/EduTable';
// import mainbaseAdd from 'common/src/views/graduateStudent/component/mainbaseAdd';
// import graduateStudent from 'common/src/api/graduateStudent/graduateStudent';
export default {
  data () {
    return {
      queryForm: {
        pageNum_: 1,
        pageSize_: 20,
        total_: 0,
        condition:{
          awardsName:'', // 竞赛级别
          projectName:'', // 竞赛项目名称
          comLevel:'', // 获奖类别
          studentId:'', // 获奖学生
          projId: '',
          awardsDateStart: '', // 获奖时间(开始)
          awardsDateEnd: '' // 获奖时间(结束)
        }
      },
      options: {
        title: [], // 获奖类别
        // profession: [], // 竞赛项目名称
        // batch: [] // 竞赛级别
      },
        pickerOptionsStart: {
          disabledDate: time => {
          let endDateVal = this.queryForm.condition.awardsDateEnd;
          if (endDateVal) {
              // return time.getTime() > endDateVal;
              return time.getTime() >= new Date(endDateVal).getTime();
          }
        }
      },
      pickerOptionsEnd: {
          disabledDate: time => {
          let beginDateVal = this.queryForm.condition.awardsDateStart;
          if (beginDateVal) {
              // return time.getTime() < beginDateVal;
              return time.getTime() <= new Date(beginDateVal).getTime()- 86400000;
          }
        }
      },
      count: 0,
      count2: 0,
      titleList: '',
      fileList: [],
      category: { category: 1 },
      mainbase: '专家',
      showDate: true,
      addflag: false,
      tableFlag: false,
      peopleadd: {},
      dataQueryForm: {},
      curRow: {},
      ifDetials: false,
      copyDialog: false,
      curCalendarId: '',
      isAllDel: true,
      exportButton: false,
      exportResult: null,
      checkTopicListForm: {
        dataList: []
      },
      multipleSelection: [],
      provideData: {}, // 提供子组件表格数据
      dataListProp: {},
      dataProp: '',
      copyTime: {
        time: ''
      },
      queryList: {
        calendarId: '',
        faculty: '',
        status: '',
        course: ''
      },
      id: '',
      formsLearn: [],
      dataList: [],
      breadPath: {
        label: this.$t('graduate.graduate'),
        key: "1",
        children: [
          {
            key: "2",
            label: ""
          }
        ]
      },
    }
  },
  mounted() {
    this.getOptions() 
    this.postStudentInform(this.queryForm)
  },
  methods: {
    // 字典获取
    getOptions() {
      // 筛选条件字典查询
      let data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: ["X_PYCC", 'X_PYLB', 'X_XWLX', 'X_XXXS', 'X_YX','JSGL_JSJB']
      };
      queryDic(data).then(res => {
        UTILS.fillSelect(
          {
            trainingCategory: "X_PYLB",
            degreeType: "X_XWLX",
            formLearning: "X_XXXS",
            faculty: "X_YX",
            TrainingLevel: "X_PYCC",
            title: 'JSGL_JSJB'
          },
          res.data,
          this.options
        );
      });
    },
    postStudentInform (data) {
      let _this = this;
      // _this.options.profession = []
      // _this.options.title = []
      // this.options.batch = [],
      competiManagementApi.queryStudentInformation(data).then(res => {
        // console.log(res,'res')
        _this.dataList = res.data.list
        if (_this.dataList) {
          _this.dataList.map(el => {
            // this.count = 0;
            // _this.options.title.forEach((item,index) => {
            //   if(item.value === el.comLevel) {
            //     ++this.count    
            //   }
            // })
            // if(this.count < 1) {
            //   _this.options.title.push({
            //     label: el.comLevel,
            //     value: el.comLevel
            //   })
            // }
            // this.count2 = 0;
            // _this.options.profession.forEach((item2,index) => {
            //   if(item2.value === el.projectName) {
            //       ++this.count2
            //   }
            // })
            // if(this.count2 < 1) {
            //   _this.options.profession.push({
            //     label: el.projectName,
            //     value: el.projectName
            //   })
            // }
          })
        }
        this.queryForm.total_ = res.data.total_
      })
    },
    // 筛选
    onSubmit() {
      this.queryForm.pageNum_ = 1
      this.postStudentInform(this.queryForm)
    },
    // 重置
    reset() {
      this.queryForm.condition = {}
      this.queryForm.pageNum_ = 1 
      this.postStudentInform(this.queryForm)
    },
    // collegeChange() {

    // },
    // 轮询直到获取key值的接口返回
    cycleToGetKey() {
      setTimeout(() => {
        // let currentIds = [];
        // if (this.multipleSelection.length > 0) {
        //   this.multipleSelection.forEach((element) => {
        //     currentIds.push(element.id);
        //   });
        // }
        competiManagementApi.downloadInformation({
          // "year": new Date().getFullYear(),
          "comLevel": this.queryForm.condition.comLevel,
          "projectName": this.queryForm.condition.projectName,
          "awardsName": this.queryForm.condition.awardsName,
          "studentId": this.queryForm.condition.studentId
        }).then(res => {
          if (res.success) {
            this.$refs.downloadRef.close();
            this.exportButton = false;
            this.normalDelayRequest(res.key, res.msg);
          } else {
            this.cycleToGetKey();
          }
        });
      }, 3000);
    },
    // 导出异步
    delayRequest(key, name) {
      setTimeout(() => {
        competiManagementApi.downloadGetKey(key, name).then(res => {
          this.exportResult = res.data;
          if (res.code == 200 && res.data.status) {
            this.exportButton = false;
            let path =
              '/api/competitionservice/competitionExport/file?path=' +
              res.data.path + '&name=' + name;
            // window.open(path);
            window.location.href = path;
            this.$message.success(this.$t('result.exportSuccessfully'));
            this.$refs.downloadRef.close();
          } else {
            this.delayRequest(key, name);
          }
        });
      }, 3000);
    },
    // Normal导出异步
    normalDelayRequest(key, name) {
      setTimeout(() => {
        competiManagementApi.downloadGetKey(key).then(res => {
          this.exportResult = res.data;
          if (res.code == 200 && res.data.status) {
            this.exportButton = false;
            let path = 
              '/api/competitionservice/competitionExport/file?path=' +
              res.data.path + '&name=' + name;
            window.location.href = path;
            this.$message.success(this.$t('result.exportSuccessfully'));
            this.$refs.downloadRef.close();
          } else {
            this.delayRequest(key, name);
          }
        });
      }, 3000);
    },
    // 导出
    exportFile() {
      // 1、第一步：调该接口，获取key值以及msg中的文件名
      // 2、第二步：将key值放入接口传参
      // 3、第三步：传入path值以及name(msg中的文件名)值，下载文件
      this.exportResult = null;
      this.$refs.downloadRef.open();
      this.exportButton = true;
      this.cycleToGetKey();
    },
    handleSelectionChange() {

    },
    // 分页
    handleSizeChange(val) {
      this.queryForm.pageSize_ = val
      this.queryForm.pageNum_ = 1
      this.postStudentInform(this.queryForm)
    },
    // 分页
    handleCurrentChange(val) {
      this.queryForm.pageNum_ = val
      this.postStudentInform(this.queryForm)
    }
  }
}
</script>
<style scoped>

</style>